﻿Ext.define('app.prm.project.EditProduct',
{
    extend: 'Ext.window.Window',
    title: '选择产品',
    scrollable: 'y',
    modal: true,
    width: 800,
    height: 600,
    layout: 'border',
    productId: 0,
    subId: 0,
    hiddenNo: false,
    initComponent: function () {
        var me = this;
        me.items = [
            {
                xtype: 'panel',
                region: 'west',
                title: '化合物检索',
                width: 500,
                split: true,
                collapsible: true,
                collapsed: true,
                border: false,
                layout: 'border',
                items: [
                    me.getSearchPanel(),
                    {
                        xtype: 'panel',
                        border: false,
                        region: 'center',
                        layout: 'fit',
                        items: [
                            me.getGrid()
                        ]
                    }
                ]
            },
            {
                region: 'center',
                border: false,
                layout: 'border',
                items: [
                    me.getChemPanel(),
                    me.getChemForm()
                ]
            }
        ];
        me.buttons = [
            { text: '保存', scope: this, handler: me.onSubmit },
            { text: '关闭', handler: function () { me.close(); } }
        ]
        me.callParent();
    },
    getSearchPanel: function () {
        var me = this;
        if (!this.searchPanel) {
            this.searchPanel = new Ext.panel.Panel({
                border: false,
                region: 'north',
                height: 50,

                bodyStyle: 'padding:5px',
                layout: 'hbox',
                items: [
                    {
                        flex: 6.7,
                        xtype: 'textfield',
                        name: 'txt_Search',
                        //id: 'txt_Search',
                        anchor: '100%',
                        emptyText: '中文名、英文名、CAS'
                    },
                    {
                        flex: 1,
                        xtype: 'button',
                        style: 'margin:0 5px 0 5px',
                        text: '查询',
                        handler: function () {
                            var keyword = me.searchPanel.query("textfield[name=txt_Search]")[0].getValue();
                            me.getGrid().store.filter = { keyword: keyword };
                            me.getGrid().store.load();
                        }
                    }
                ]
            });
        }
        return this.searchPanel;
    },
    getGrid: function () {
        if (!this.gridPanel) {
            var me = this;
            var store = Ext.create("app.ux.PagePrmStore", { url: '/api/services/app/substance/GetAll' });
            this.gridPanel = new Ext.grid.Panel({
                forceFit: true,
                store: store,
                border: true,
                bbar: new Ext.toolbar.Paging({
                    store: store,
                    displayInfo: true,
                    displayMsg: "显示{0}-{1}条,共计{2}条",
                    emptyMsg: "没有数据"
                }),
                columns: [
                   {
                       header: '结构式',
                       sortable: false,
                       dataIndex: 'molPngBase64'
                    , align: 'center',
                       renderer: function (value, p, record) {

                           if (value && value.length > 10) {
                               return '<img src="data:image/png;base64,' + value + '" onload="if(this.height >= 50||this.width >= 100){ if((this.height*2) >this.width) {this.height = 50}else {this.width=100}}" >'
                           }


                       }
                   },
                    { header: "中文名", dataIndex: 'cnName' },
                    { header: "英文名", dataIndex: 'enName' },
                    { header: "CAS", dataIndex: 'cas' },
                    //{ header: "分子式", dataIndex: 'formula' },
                    //{ header: "分子量", dataIndex: 'molecularWeight' },
                    //{ header: "高风险", dataIndex: 'highRisk' }
                ],
                listeners: {
                    itemclick: function (grid, record, item, index, e, eOpts) {
                        me.loadChemInfo(record.get('id'), 0);
                    }
                }
            });
        }
        return this.gridPanel;
    },
    loadChemInfo: function (subId, productId) {
        var me = this;
        App.AjaxGet('/api/services/app/substance/GetById?id=' + subId + '&productId=' + productId, function (data) {
            var json = data.result;
            me.chemForm.getForm().setValues(
            {
                SubstanceId: json.id,
                CAS: json.cas,
                Formula: json.formula,
                MolecularWeight: json.molecularWeight,
                CnName: json.cnName,
                EnName: json.enName,
                HighRisk: json.highRisk,
                Note: json.note
            });
            if (productId > 0)
            {
                me.chemForm.getForm().setValues(
                {

                    ProductId: json.productId,
                    ProductNo: json.productNo,
                });
            }
            //me.chemForm.query("combo[name=ProductNo]")[0].store.loadData(json.products);
            me.getChemPanel().mol = json.mol;
            me.getChemPanel().img = json.molPngBase64;
            me.getChemPanel().cdx = json.cdx;
            me.getChemPanel().mf = json.formula;
            me.getChemPanel().mw = json.molecularWeight;
            me.getChemPanel().showImg();
            //if (json.products && json.products.length > 0) {
            //    me.chemForm.query("combo[name=ProductNo]")[0].setRawValue(json.products[0].code);
            //    me.chemForm.query("combo[name=ProductNo]")[0].setValue(json.products[0].id);
            //    me.chemForm.query("hidden[name=ProductId]")[0].setValue(json.products[0].id);
            //}
        });
    },
    getChemPanel: function () {
        if (!this.chemPanel) {
            var me = this;
            this.chemPanel = Ext.create("app.ux.GSE.gsePanel", {
                region: 'north',
                height: 240,
                mol: '',
                img: '',
                listeners: {
                    afterCommit: function (mf, mw) {
                        console.log('afterCommit>>mw,mf');
                        var form = me.getChemForm().getForm();
                        form.setValues({ MolecularWeight: mw, Formula: mf });
                    }
                }
            });
        }
        return this.chemPanel;
    },
    getChemForm: function () {
        var me = this;
        if (!this.chemForm) {
            this.chemForm = new Ext.form.Panel({
                defaultType: 'textfield',
                border: false,
                title: '物质信息',
                region: 'center',
                scrollable: 'y',
                fieldDefaults: {
                    labelWidth: 90,
                    labelAlign: "right",
                    flex: 1,
                    margin: 6
                },
                items: [
                    {
                        xtype: 'container',
                        anchor: '100%',
                        layout: 'hbox',
                        items: [
                            {
                                xtype: 'container',
                                flex: 1,
                                layout: 'anchor',
                                items: [
                                    {
                                        xtype: 'hidden',
                                        name: 'SubstanceId',
                                        value: 0
                                    },
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: 'CAS',
                                        //id: 'txt_Cas',
                                        name: 'CAS'
                                    },
                                    {
                                        xtype: 'textfield',
                                        //id: 'txt_Formula',
                                        fieldLabel: '分子式',
                                        name: 'Formula'
                                    }
                                ]
                            },
                            {
                                xtype: 'container',
                                flex: 1,
                                layout: 'anchor',
                                items: [
                                    {
                                        xtype: 'hidden',
                                        name: 'ProductId',
                                        value: 0
                                    }
                                    ,
                                    {
                                        xtype: 'textfield',
                                        hidden: me.hiddenNo,
                                        fieldLabel: '产品号',
                                        name: 'ProductNo'
                                    }
                                    ,
                                    {
                                        xtype: 'textfield',
                                        //id: 'txt_MolecularWeight',
                                        fieldLabel: '分子量',
                                        name: 'MolecularWeight'
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        xtype: 'textarea',
                        fieldLabel: '中文名',
                        height: 50,
                        //id: 'txt_CnName',
                        anchor: '75%',
                        name: 'CnName'
                    },
                    {
                        xtype: 'textarea',
                        fieldLabel: '英文名',
                        height: 50,
                        anchor: '75%',
                        // id: 'txt_EnName',
                        name: 'EnName'
                    },
                    {
                        xtype: 'textarea',
                        fieldLabel: '高风险',
                        height: 50,
                        // id: 'txt_HighRisk',
                        anchor: '75%',
                        name: 'HighRisk'
                    },
                    {
                        xtype: 'textarea',
                        fieldLabel: '备注',
                        height: 50,
                        //  id: 'txt_Note',
                        anchor: '75%',
                        name: 'Note'
                    }
                ]
            });
        }
        return this.chemForm;
    },
    onSubmit: function () {
        var me = this;
        var form = me.getChemForm().getForm().getValues();
        form.Mol = me.getChemPanel().mol;
        form.MolPngBase64 = me.getChemPanel().img;
        form.CDX = me.getChemPanel().cdx;
        form.hiddenNo = me.hiddenNo;
        //form.ProductNo = me.chemForm.query("combo[name=ProductNo]")[0].getRawValue();
        App.AjaxPost('/api/services/app/substance/SetSubstanceProduct', form, function (data) {
            console.log(data);
            var productNo = form.productNo;
            me.fireEvent("savesuccess", productNo, data.result);
        });
    },
    listeners: {
        afterrender: function (win) {
            if (win.subId > 0)
            {
                win.loadChemInfo(win.subId, win.productId);
            }
        }
    }
});